﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class LapPhieuNhapKho : UserControl
    {
        public LapPhieuNhapKho()
        {
            InitializeComponent();
        }

        private void LapPhieuNhapKho_Load(object sender, EventArgs e)
        {
            loadData();
            searchCollapseBtn_Click(null, null);
        }

        private void loadData()
        {
            searchPhieuNhapKho1.refreshGrid();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            OtherForms.ThemPhieuNhapKho f = new OtherForms.ThemPhieuNhapKho();
            f.ShowDialog();
            loadData();
        }

        private void dgvPhieuNhapKho_DoubleClick(object sender, EventArgs e)
        {
            if (dgvPhieuNhapKho.SelectedRows.Count <= 0)
                return;

            string maPhieu = (string) dgvPhieuNhapKho.SelectedRows[0].Cells[1].Value;
            OtherForms.XemPhieuNhapKho f = new OtherForms.XemPhieuNhapKho(maPhieu);
            f.Show();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvPhieuNhapKho.SelectedRows.Count <= 0)
                return;

            string maPhieu = (string)dgvPhieuNhapKho.SelectedRows[0].Cells[1].Value;
            OtherForms.ThemPhieuNhapKho f = new OtherForms.ThemPhieuNhapKho(maPhieu);
            f.ShowDialog();
            loadData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuNhapKho.SelectedRows.Count <= 0)
                return;

            // check trang thai
            string maPhieu = (string)dgvPhieuNhapKho.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.PhieuNhapKho p = dc.PhieuNhapKhos.Single(x => x.MaPhieu == maPhieu);
            _DB.QuyTrinhMuaHang qt = p.DonDatHang.QuyTrinhMuaHang;
            if (qt.TrangThai !=  _DB.TrangThaiQuyTrinhMuaHang.DANG_NHAP_KHO &&
                qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.DA_NHAP_KHO)
            {
                return;
            }

            // confirm
            if (MessageBox.Show("Chắn chắn?", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            p.deleted = true;
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DANG_NHAP_KHO;
            dc.SubmitChanges();
        }

        private void searchCollapseBtn_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
        }

    }
}
